package abg.manage.controller.sys;

import abg.core.common.security.AuthUser;
import abg.core.common.security.AuthKit;
import abg.core.domain.db.SysFile;
import abg.core.domain.sys.FileBType;
import abg.core.domain.search.Psh;
import abg.core.domain.sys.SRS;
import abg.core.service.sys.SysFileService;
import com.mybatisflex.core.paginate.Page;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * @author leyili
 * 文件管理
 */
@Slf4j
@RestController
@RequestMapping("/manage/file")
public class SysFileController {

    @Resource
    private SysFileService sysFileService;



    /**
     * 上传图片-file
     */
    @PostMapping(value = "/uploadImgFile")
    public SRS<SysFile> uploadImgFile(@RequestPart("file") MultipartFile file, @RequestHeader String token, @RequestHeader FileBType bType) {
        AuthUser currentUser = AuthKit.mastLogin(token);
        SysFile sysFile = sysFileService.uploadImgFile(file, currentUser.getUserId(), bType);
        return SRS.bySuccess(sysFile);
    }

    /**
     * 上传图片-base64
     */
    @PostMapping(value = "/uploadImgBase64")
    public SRS<Object> uploadImgBase64(String base64, @RequestHeader String token, @RequestHeader FileBType bType) {
        AuthUser currentUser = AuthKit.mastLogin(token);
        SysFile sysFile = sysFileService.uploadImgFile(base64, currentUser.getUserId(), bType);
        return SRS.bySuccess(sysFile);
    }


    /**
     * 查询文件列表
     *
     * @param sysFile
     * @param psh
     * @return
     */
    @PostMapping("/page")
    public SRS<Page<SysFile>> page(SysFile sysFile, Psh psh) {
        return SRS.bySuccess(sysFileService.page(sysFile, psh));
    }


}
